var flowApi = require('../../script/flow.js');
var app = getApp();
var config = require('../../script/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: null,
    InsName: '',
    input_timeOut: null,
    ActList: [],
    PageInfo: {
      Sum: 0,
      PageSize: 15,
      CurrPage: 1,
      Sort: ""
    },
    canLoadData: true,
    noData: false,
    isShowConfirm: false,
    BackReason: '',
    detailPopShow: false,
    InsInfo: {
      InsUpdaterName: '',
      InsStatusName: '',
      InsUpdateNodeName: '',
      InsUpdateTime: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.preloadWebview?.()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo()
    this._refreshData();
    this.getActList(true)
  },
  _refreshData: function () {
    this.setData({ //初始化数据
      InsName: '',
      input_timeOut: null,
      PageInfo: {
        Sum: 0,
        PageSize: 15,
        CurrPage: 1,
        Sort: ""
      },
      canLoadData: true,
      noData: false,
      isShowConfirm: false,
      CurrFlow: null,
      detailPopShow: false,
      InsInfo: {
        InsUpdaterName: '',
        InsStatusName: '',
        InsUpdateNodeName: '',
        InsUpdateTime: ''
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
    this.getActList(true)
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.canLoadData) {
      this.setData({
        'PageInfo.CurrPage': this.data.PageInfo.CurrPage + 1
      })
      this.getActList();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 1000
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getUserInfo: function () {
    try {
      var value = wx.getStorageSync(config.storageKey.userinfo)
      if (value) {
        this.setData({
          userinfo: value
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  inputChange(e) {
    this._refreshData();
    let {
      input_timeOut
    } = this.data
    if (input_timeOut) {
      clearTimeout(input_timeOut)
    }
    input_timeOut = setTimeout(() => {
      this.setData({
        InsName: e.detail.value
      })
      this.getActList(true)
    }, 300);
    this.setData({
      input_timeOut
    })
  },
  getActList(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let params = {
      SystemMark: 'XtYthHeat',
      accessToken: wx.getStorageSync(config.storageKey.token),
      PageInfo: this.data.PageInfo,
      Entity: {
        GroupeCode: "",
        StaCreateTime: "",
        EndCreateTime: "",
        FinishStaTime: "",
        FinishEndTime: "",
        FlowId: null,
        ACTTYPE: "",
        OrgNo: "",
        ActState: null,
        NodeId: null,
        InsName: this.data.InsName,
        FlowRemarks: null
      }
    };
    flowApi.ProcessedPage(params).then(res => {
      if (res.Result === 1) {
        let dataList = []
        if (isRefresh) {
          dataList = [...res.Datas]
        } else {
          dataList = [...this.data.ActList, ...res.Datas]
        }
        let canLoadData = res.PageInfo.PageSize * res.PageInfo.CurrPage <= res.PageInfo.Sum
        const noData = dataList.length === 0
        this.setData({
          ActList: dataList,
          canLoadData: canLoadData,
          noData: noData,
          PageInfo: res.PageInfo
        })
      }
      wx.hideLoading();
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  toDetail: function (e) {
    const item = e.currentTarget.dataset.item
    // http://36.99.164.197:8260/vue_bpm/#/wfms/run/InsActManage/ActInfoPage?accessToken=f05d07cd-3532-41c0-8eaf-4f26a7237509&FlowId=3&NodeId=5&PageType=1&InsId=&ActId=
    const url = "../actinfo/actinfo?PageType=2&InsId=" + item.InsId + "&ActId=" + item.ActId;
    wx.navigateTo({
      url: url
    })
  },

  TackBack: function (e) {
    var that = this;
    wx.showModal({
      content: '是否确定取回该流程？',
      success(res) {
        if (res.confirm) {
          that.setData({
            isShowConfirm: true,
            CurrFlow: e.currentTarget.dataset.item
          })
        }
      }
    })
  },
  FlowView(e) {
    this.setData({
      detailPopShow: true
    })
    var CurrFlow = e.currentTarget.dataset.item
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let params = {
      SystemMark: 'XtYthHeat',
      accessToken: wx.getStorageSync(config.storageKey.token),
      PageInfo: this.data.PageInfo,
      Entity: {
        InsId: CurrFlow.InsId
      }
    };
    flowApi.CurrAccountPage(params).then(res => {
      if (res.Result === 1) {
        let resData = res.Datas && res.Datas.length > 0 ? res.Datas[0] : null
        if (resData) {
          var InsInfo = {
            InsUpdaterName: resData.InsUpdaterName,
            InsStatusName: resData.InsStatusName,
            InsUpdateNodeName: resData.InsUpdateNodeName,
            InsUpdateTime: resData.InsUpdateTime
          }
          this.setData({
            InsInfo
          })
        }
      }
      wx.hideLoading();
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  //输入框中的值
  setValue: function (e) {
    this.setData({
      BackReason: e.detail.value
    })
  },
  //点击取消按钮
  cancel: function () {
    var that = this
    that.setData({
      isShowConfirm: false,
    })
  },
  //点击确认按钮
  confirmAcceptance: function () {
    var that = this;
    if (that.data.BackReason == '') {
      wx.showToast({
        title: '取回原因不能为空！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    that.setData({
      isShowConfirm: false,
    })
    wx.showLoading({
      mask: true,
      title: '处理中...'
    });
    const params = {
      SystemMark: 'XtYthHeat',
      accessToken: wx.getStorageSync(config.storageKey.token),
      wfmsToken: '',
      DingUserId: '',
      Entity: {
        ActId: that.data.CurrFlow.ActId,
        ActOperInfo: that.data.BackReason
      }
    }
    flowApi.TackBack(params).then(res => {
      if (res.Result === 1) {
        wx.showToast({
          title: res.Msg,
          icon: 'success',
          duration: 4000,
          success: function () {
            that._refreshData();
            that.getActList(true);
          }
        })
      } else {
        wx.showToast({
          title: res.Msg,
          icon: 'error',
          duration: 2000
        })
      }
    })
  },

  // 打开弹窗
  popupTap: function (e) {
    this.setData({
      detailPopShow: true
    })
  },
  closePop: function (e) {
    this.setData({
      detailPopShow: false,
      details: null
    })
  },
  // 这个函数内容为空 用于阻止屏幕滚动
  preventTouchMove: function (e) {},
})